package com.example.administrator.szb.fragments.fragmenttool.anli;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.webviews.WebViewsActivity;
import com.example.administrator.szb.adapter.Anliadapter;
import com.example.administrator.szb.bean.AnliBean;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.QTLog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.api.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by stone on 2018/1/22.
 */

public class FragmentAnli  extends MVPBaseFragment {
    private SmartRefreshLayout freshlayout;
    private ListView listView;
    View error_net_ll,noda_ll;
    private LinearLayout ll_error;
    private ImageView iv_error;
    private TextView tv_net_error;
    private TextView tv_tips;
    private Button btn_error;
    private LinearLayout no_data_ll;
    private ImageView iv_no_data;
    private TextView tv_no_data;
    private TextView tv_tips2;
    private Button no_data_btn;
    ImageView new_ask;
    private boolean isFirstEnter = true;
    int current_page = 1 ;
    int per_page = 10;
    private List lists=new ArrayList();
    private Anliadapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tmp_refresh,null);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(View view) {
        freshlayout = (SmartRefreshLayout) view.findViewById(R.id.freshlayout);
        listView = (ListView) view.findViewById(R.id.listView);
        listView.setBackgroundColor(Color.parseColor("#F3F3F3"));
        ll_error = (LinearLayout) view.findViewById(R.id.ll_error);
        iv_error = (ImageView) view.findViewById(R.id.iv_error);
        tv_net_error = (TextView) view.findViewById(R.id.tv_net_error);
        tv_tips = (TextView) view.findViewById(R.id.tv_tips);
        btn_error = (Button) view.findViewById(R.id.btn_error);
        no_data_ll = (LinearLayout) view.findViewById(R.id.no_data_ll);
        iv_no_data = (ImageView) view.findViewById(R.id.iv_no_data);
        tv_no_data = (TextView) view.findViewById(R.id.tv_no_data);
        tv_tips2 = (TextView) view.findViewById(R.id.tv_tips2);
        no_data_btn = (Button) view.findViewById(R.id.no_data_btn);
        noda_ll = view.findViewById(R.id.noda_ll);
        error_net_ll = view.findViewById(R.id.error_net_ll);
    }
    @Override
    protected void initEvent() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, WebViewsActivity.class);
                intent.putExtra("url",URLAddress.ANLI_URL+((AnliBean)lists.get(position)).getId());
                intent.putExtra("bar_title","案例详情");
                intent.putExtra("show",true);
                intent.putExtra("type",3);
                intent.putExtra("id",((AnliBean)lists.get(position)).getId());
                intent.putExtra("titles",((AnliBean)lists.get(position)).getTitle());
                intent.putExtra("contents",((AnliBean)lists.get(position)).getContent());
                startActivity(intent);
            }
        });
        if (isFirstEnter) {
            isFirstEnter = false;
            freshlayout.autoRefresh();
        }
        freshlayout.setEnableLoadMore(true);//是否启用上拉加载功能
        freshlayout.setEnableLoadMoreWhenContentNotFull(true);//是否在列表不满一页时候开启上拉加载功能


        freshlayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                int p = current_page+1;
                typeReq=1;
                requestReferList(1, p,per_page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                QTLog.e("刷新");
                typeReq=0;
                requestReferList(0, 1,per_page);
            }
        });
        btn_error.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                freshlayout.autoRefresh();
                requestReferList(0, 1,per_page);
            }
        });
    }

    @Override
    protected void initData() {

        adapter = new Anliadapter(context,lists);

        listView.setAdapter(adapter);
    }
    int typeReq=0;
    /**
     *
     * @param type 其中传入0代表刷新，1代表加载更多
     * @param page
     * @param count
     */
    private void requestReferList(final int type, int page, int count){
        hideAll();
        Map<String,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("count",count);
        HttpUtils.post(activity,URLAddress.ANLI_GETALL, map, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                showList(JSON.parseArray(str, AnliBean.class));
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }


    public void showList(List list) {
        if (typeReq==1){
            //加载更多
            freshlayout.finishLoadmore();

            current_page++;
            if (list.size()<per_page){

                //没有更多了
                freshlayout.setEnableLoadMore(false);

            }
        }else {
            lists.clear();

            //刷新
            freshlayout.finishRefresh();
            freshlayout.setEnableLoadMore(true);
            current_page=1;
        }
        lists.addAll(list);
        if (lists.size()==0){
            showNodata();
        }

        adapter.notifyDataSetChanged();
    }


    public void onSuccesss(String str) {

    }


    public void onFails(String string) {
        showError();
    }
    private void showNodata(){
        freshlayout.setVisibility(View.VISIBLE);
        noda_ll.setVisibility(View.VISIBLE);
        listView.setVisibility(View.GONE);
        error_net_ll.setVisibility(View.GONE);
        QTLog.e("aaaaa");
    }
    private void hideAll(){
        noda_ll.setVisibility(View.GONE);
        freshlayout.setVisibility(View.VISIBLE);
        listView.setVisibility(View.VISIBLE);
        error_net_ll.setVisibility(View.GONE);
    }
    private void showError(){
        freshlayout.setVisibility(View.GONE);
        error_net_ll.setVisibility(View.VISIBLE);
    }
}
